<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="样品名称" prop="sampleName">
        <el-input
          v-model="queryParams.sampleName"
          placeholder="请输入样品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样品编号" prop="sampleCode">
        <el-input
          v-model="queryParams.sampleCode"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测编号" prop="testCode">
        <el-input
          v-model="queryParams.testCode"
          placeholder="请输入检测编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['soillihua:soillihua:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['soillihua:soillihua:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['soillihua:soillihua:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['soillihua:soillihua:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="soillihuaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="样品名称" align="center" prop="sampleName" />
      <el-table-column label="样品编号(施肥量%-样区)" align="center" prop="sampleCode" />
      <el-table-column label="检测编号(F-年月日-001)" align="center" prop="testCode" />
      <el-table-column label="样品状态" align="center" prop="sampleStatus" />
      <el-table-column label="样品重量" align="center" prop="sampleWeight" />

      <el-table-column label="检验项目" align="center">
        <template slot-scope="scope">
          <el-tooltip effect="dark" :content="scope.row.testItems" placement="top">
            <span>{{
                scope.row.testItems.slice(0, 10)
              }}{{ scope.row.testItems.length > 10 ? '...' : '' }}</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="pH值" align="center" prop="phValue" />
      <el-table-column label="有机质(g/kg)" align="center" prop="organicMatter" />
      <el-table-column label="全氮(%)" align="center" prop="totalNitrogen" />
      <el-table-column label="铵态氮(mg/kg)" align="center" prop="ammoniumNitrogen" />
      <el-table-column label="硝态氮(mg/kg)" align="center" prop="nitrateNitrogen" />
      <el-table-column label="有效磷(mg/kg)" align="center" prop="availablePhosphorus" />
      <el-table-column label="速效钾(mg/kg)" align="center" prop="availablePotassium" />
      <el-table-column label="水溶性钾离子(cmol/kg)" align="center" prop="waterSolublePotassium" />
      <el-table-column label="交换性钙(cmol/kg)" align="center" prop="exchangeableCalcium" />
      <el-table-column label="交换性镁(cmol/kg)" align="center" prop="exchangeableMagnesium" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['soillihua:soillihua:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['soillihua:soillihua:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改土壤样品检测结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="样品名称" prop="sampleName">
          <el-input v-model="form.sampleName" placeholder="请输入样品名称" />
        </el-form-item>
        <el-form-item label="样品编号" prop="sampleCode">
          <el-input v-model="form.sampleCode" placeholder="请输入样品编号" />
        </el-form-item>
        <el-form-item label="检测编号" prop="testCode">
          <el-input v-model="form.testCode" placeholder="请输入检测编号" />
        </el-form-item>
        <el-form-item label="样品重量" prop="sampleWeight">
          <el-input v-model="form.sampleWeight" placeholder="请输入样品重量" />
        </el-form-item>
        <el-form-item label="检验项目" prop="testItems">
          <el-input v-model="form.testItems" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="pH值" prop="phValue">
          <el-input v-model="form.phValue" placeholder="请输入pH值" />
        </el-form-item>
        <el-form-item label="有机质(g/kg)" prop="organicMatter">
          <el-input v-model="form.organicMatter" placeholder="请输入有机质(g/kg)" />
        </el-form-item>
        <el-form-item label="全氮(%)" prop="totalNitrogen">
          <el-input v-model="form.totalNitrogen" placeholder="请输入全氮(%)" />
        </el-form-item>
        <el-form-item label="铵态氮(mg/kg)" prop="ammoniumNitrogen">
          <el-input v-model="form.ammoniumNitrogen" placeholder="请输入铵态氮(mg/kg)" />
        </el-form-item>
        <el-form-item label="硝态氮(mg/kg)" prop="nitrateNitrogen">
          <el-input v-model="form.nitrateNitrogen" placeholder="请输入硝态氮(mg/kg)" />
        </el-form-item>
        <el-form-item label="有效磷(mg/kg)" prop="availablePhosphorus">
          <el-input v-model="form.availablePhosphorus" placeholder="请输入有效磷(mg/kg)" />
        </el-form-item>
        <el-form-item label="速效钾(mg/kg)" prop="availablePotassium">
          <el-input v-model="form.availablePotassium" placeholder="请输入速效钾(mg/kg)" />
        </el-form-item>
        <el-form-item label="水溶性钾离子(cmol/kg)" prop="waterSolublePotassium">
          <el-input v-model="form.waterSolublePotassium" placeholder="请输入水溶性钾离子(cmol/kg)" />
        </el-form-item>
        <el-form-item label="交换性钙(cmol/kg)" prop="exchangeableCalcium">
          <el-input v-model="form.exchangeableCalcium" placeholder="请输入交换性钙(cmol/kg)" />
        </el-form-item>
        <el-form-item label="交换性镁(cmol/kg)" prop="exchangeableMagnesium">
          <el-input v-model="form.exchangeableMagnesium" placeholder="请输入交换性镁(cmol/kg)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSoillihua, getSoillihua, delSoillihua, addSoillihua, updateSoillihua } from "@/api/soillihua/soillihua";

export default {
  name: "Soillihua",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 土壤样品检测结果表格数据
      soillihuaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampleName: null,
        sampleCode: null,
        testCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sampleName: [
          { required: true, message: "样品名称不能为空", trigger: "blur" }
        ],
        sampleCode: [
          { required: true, message: "样品编号不能为空", trigger: "blur" }
        ],
        testCode: [
          { required: true, message: "检测编号不能为空", trigger: "blur" }
        ],
        sampleStatus: [
          { required: true, message: "样品状态不能为空", trigger: "change" }
        ],
        sampleWeight: [
          { required: true, message: "样品重量不能为空", trigger: "blur" }
        ],
        testItems: [
          { required: true, message: "检验项目不能为空", trigger: "blur" }
        ],
        phValue: [
          { required: true, message: "pH值不能为空", trigger: "blur" }
        ],
        organicMatter: [
          { required: true, message: "有机质(g/kg)不能为空", trigger: "blur" }
        ],
        totalNitrogen: [
          { required: true, message: "全氮(%)不能为空", trigger: "blur" }
        ],
        ammoniumNitrogen: [
          { required: true, message: "铵态氮(mg/kg)不能为空", trigger: "blur" }
        ],
        nitrateNitrogen: [
          { required: true, message: "硝态氮(mg/kg)不能为空", trigger: "blur" }
        ],
        availablePhosphorus: [
          { required: true, message: "有效磷(mg/kg)不能为空", trigger: "blur" }
        ],
        availablePotassium: [
          { required: true, message: "速效钾(mg/kg)不能为空", trigger: "blur" }
        ],
        waterSolublePotassium: [
          { required: true, message: "水溶性钾离子(cmol/kg)不能为空", trigger: "blur" }
        ],
        exchangeableCalcium: [
          { required: true, message: "交换性钙(cmol/kg)不能为空", trigger: "blur" }
        ],
        exchangeableMagnesium: [
          { required: true, message: "交换性镁(cmol/kg)不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询土壤样品检测结果列表 */
    getList() {
      this.loading = true;
      listSoillihua(this.queryParams).then(response => {
        this.soillihuaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sampleName: null,
        sampleCode: null,
        testCode: null,
        sampleStatus: null,
        sampleWeight: null,
        testItems: null,
        phValue: null,
        organicMatter: null,
        totalNitrogen: null,
        ammoniumNitrogen: null,
        nitrateNitrogen: null,
        availablePhosphorus: null,
        availablePotassium: null,
        waterSolublePotassium: null,
        exchangeableCalcium: null,
        exchangeableMagnesium: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加土壤样品检测结果";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSoillihua(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改土壤样品检测结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSoillihua(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSoillihua(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除土壤样品检测结果编号为"' + ids + '"的数据项？').then(function() {
        return delSoillihua(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('soillihua/soillihua/export', {
        ...this.queryParams
      }, `soillihua_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
